package com.codefish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codefish.entity.PerInformationEntity;
import com.codefish.mapper.PerInformationDao;
import com.codefish.service.PerInformationService;
import com.codefish.util.IdWorker;
import com.codefish.util.R;
import com.codefish.util.SmsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Classname PerInformationServiceImpl
 * @Description 用户管理
 * @Date 2021/4/29 15:08
 * @Created 学兔斯基眯眼看世界
 */
@Service
public class PerInformationServiceImpl extends ServiceImpl<PerInformationDao, PerInformationEntity> implements PerInformationService {

    private static final Logger log = LoggerFactory.getLogger(PerInformationServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SmsUtil smsUtil;

    @Autowired
    private IdWorker idWorker;

    /**
     * 分页条件查询
     *
     * @param name
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public IPage<PerInformationEntity> getPageByParams(String name, int currentPage, int size) {

        // 分页
        Page<PerInformationEntity> page = new Page<>(new Long(currentPage), new Long(size));

        // 构建查询条件
        QueryWrapper<PerInformationEntity> queryWrapper = new QueryWrapper<PerInformationEntity>().eq("is_del", 0);

        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like("name", name);
        }

        IPage<PerInformationEntity> entityIPage = baseMapper.selectPage(page, queryWrapper);

        return entityIPage;
    }

    /**
     * 根据联系方式和登录code查询个数
     *
     * @param perInformationEntity
     * @return
     */
    @Override
    public boolean queryCountByTelephoneAndUsername(PerInformationEntity perInformationEntity) {
        int count = baseMapper.queryCountByTelephoneAndUsername(perInformationEntity);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 逻辑删除 true:删除成功,false:删除失败
     *
     * @param id
     */
    @Override
    public boolean deleteOne(Long id) {
        int delete = baseMapper.updateStatusAndIsDel(id);
        log.info("逻辑删除影响行数:" + delete);
        if (delete > 0) {
            return true;
        }
        return false;
    }

    @Override
    public R sendSms(String phone) {
        //1.生成一个6位的短信验证码

        Random random = new Random();
        int code = random.nextInt(999999);  //生成的验证码
        if (code < 100000) { //位数处理
            code = code + 100000;
        }
        log.info("短信验证码:{}", code);

        //2.将验证码保存到redis中
        redisTemplate.boundValueOps("code_" + phone).set(code + "");
        redisTemplate.boundValueOps("code_" + phone).expire(5, TimeUnit.MINUTES);//5分钟过期
        //3.将验证码发送到mq

        return smsUtil.sendSms(phone, code + "");

    }

    @Override
    public R registrationUser(PerInformationEntity perInformationEntity, String smsCode) {
        String telephone = perInformationEntity.getTelephone();

        //1.校验
        String sysCode = (String) redisTemplate.boundValueOps("code_" + telephone).get();//提取系统的验证码
        if (sysCode == null) {
            return R.error("验证码未发送或已过期");
        }
        if (!sysCode.equals(smsCode)) {
            return R.error("验证码不正确");
        }

        // 校验该手机号是否已经注册
        int queryByTelephone = baseMapper.queryByTelephone(telephone);

        if (queryByTelephone > 0) {
            return R.error("该手机号已经注册");
        }

        perInformationEntity.setId(idWorker.nextId());
        baseMapper.insert(perInformationEntity);

        return R.ok();
    }
}
